#! /bin/sh
# 
# (C) 2006 by Argonne National Laboratory.
#     See COPYRIGHT in top-level directory.
#
# Update all of the derived files
# For best performance, execute this in the top-level directory.
# There are some experimental features to allow it to be executed in
# subdirectories
#
# Eventually, we want to allow this script to be executed anywhere in the
# mpich tree.  This is not yet implemented.
error_summary=""
acVersion=unknown

# finalStatus records whether there have been problems in updatefiles
# 1 = warning, 3 = fatal, 2 = reserved for future use, 
# 0 = no problems detected.
finalStatus=0
at_top=no
if [ -d maint -a -s maint/f77tof90.in ] ; then
    at_top=yes
fi
#
# Default choices
do_f77tof90=yes
do_build_configure=yes
do_makefiles=yes

# Allow MAKE to be set from the environment
MAKE=${MAKE-make}

# List of steps that we will consider
# (We do not include depend because the values for depend are not just yes/no)
AllSteps="f77tof90 build_configure makefiles"
stepsCleared=no

# Options for some steps
autoconfdir=""
# Extract the arguments intended for updatefiles.
temp_args=""
for arg in "$@" ; do
    case $arg in 
    -echo)
	set -x
	;;

    -do=*)
    opt=`echo A$arg | sed -e 's/A-do=//'`
    # Handle some synonyms
    case $opt in 
	build-configure|configure) opt=build_configure ;;
	makefile|Makefile|Makefiles) opt=makefiles;;
    esac
    var=do_$opt
    # Check that this opt is known
    eval oldval=\$"$var"
    if [ -z "$oldval" ] ; then
	echo "-do=$opt is unrecognized"
	exit 1
    else 
        if [ $stepsCleared = no ] ; then
	    for step in $AllSteps ; do
	        var=do_$step
	        eval $var=no
	    done
	    stepsCleared=yes
        fi
        var=do_$opt
        eval $var=yes
    fi
    ;;

    -with-autoconf=*|--with-autoconf=*)
    # Select a location for a different autoconf
    autoconfdir=`echo "A$arg" | sed -e 's/.*=//'`
    ;;

    -distrib)
    do_build_configure=no
    temp_args="$temp_args $arg"
    ;;

    -help|--help|-usage|--usage)
    cat <<EOF
    updatefiles [ --with-autoconf=dir ] [ -do=stepname ]

    Update the files in the MPICH build tree.  This file builds the 
    configure files, creates the Makefile.in files (using the autotools),
    and extracts the error messages.

    You can use --with-autoconf=dir to specify a directory that contains
    an alternate autoconf (and especially autoreconf).

    Use -do=stepname to update only a single step.  For example, 
    -do=build_configure only updates the configure scripts.  The available
    steps are
    $AllSteps
EOF
    exit
    ;;
    *)
    temp_args="$temp_args $arg"
    ;;
    esac
done
# Reset the arguments.  Note that this doesn't handle arguments that contain
# blanks.  I hope that we don't need those.
set -- $temp_args
#
#
if [ $at_top = "no" ] ; then
    echo "Must execute at top level directory for now"
    exit 1
fi
# Determine the autoconf to use.  If --with-autoconf was set, use 
# autoconf and autoheader from that directory
# This may also be needed for tools in the maint directory
if [ -n "$autoconfdir" ] ; then
    if [ -x $autoconfdir/autoreconf ] ; then
        autoreconf=$autoconfdir/autoreconf
        AUTORECONF=$autoreconf
        export AUTORECONF
    else
        echo "Could not find executable autoreconf in $autoconfdir"
	exit 1
    fi
else
    autoreconf=${AUTORECONF:-autoreconf}
fi
#
# Check that you have a working autoconf.  Autoconf 2.57 is not compatible with
# previous versions of autoconf (!!), even 2.52 (!!!).  
acVersion=ok
if [ -d .tmp ] ; then rm -rf .tmp ; fi
if [ -s .tmp ] ; then rm -f .tmp ; fi
if [ ! -d .tmp ] ; then
    mkdir .tmp 2>&1 >/dev/null
fi

# we require autoconf version 2.67 or greater
# acSubversion is the version number minus 2.  (we assume autoconf 2.xx)
# -1 is used for unknown
acSubversion=-1
acIncDirFlag=-I
for ver in 70 69 68 67 ; do
    rm -f .tmp/configure.ac .tmp/configure
    cat >.tmp/configure.ac <<EOF
AC_PREREQ(2.$ver)
EOF
    if (cd .tmp && $autoconf >/dev/null 2>&1 ) ; then
	acSubversion=$ver
	break
    fi
done
rm -f .tmp/configure.ac .tmp/configure
if [ "$acSubversion" -gt 0 ] ; then
    acVersion="2.$acSubversion"
    echo "You have autoconf version $acVersion."
else
    cat <<EOF
You either do not have autoconf in your path or updatefiles was unable to 
determine which version of autoconf you have.  You may be able to use
     autoconf --version
to see the version of autoconf (unfortunately, there is no standard 
format for the version output and it changes between autoconf versions.
In addition, some versions of autoconf choose among many versions and
provide incorrect output).
EOF
	error_summary="$error_summary \
No autoconf in path or unable to determine the version of autoconf."
        acVersion="Unknown"
fi
rm -rf .tmp

#
# Build scripts such as f77tof90 if necessary
run_configure=no

# Later versions of autoconf put the autoconf version into the autom4te*.cache
# name.
# Later versions of autoconf (2.57+?) will silently ignore the command to 
# rebuild the configure if it thinks that nothing has changed.  However,
# it does not accurately decide this (e.g., if aclocal.m4 includes files 
# that have changed, autoconf will ignore that and not regenerate the 
# configure file).  The information that autoconf uses is saved in the
# autom4te*.cache file; since this cache is not accurate, we delete it.
if [ ! -x maint/configure ] ; then
    (cd maint && $autoconf && rm -rf autom4te*.cache )
elif find maint -name 'configure.ac' -newer 'maint/configure' >/dev/null 2>&1 ; then
    # The above relies on the Unix find command
    (cd maint && $autoconf && rm -rf autom4te*.cache)
fi
if [ ! -x maint/f77tof90 ] ; then
    run_configure=yes
fi
#
# The following relies on the Unix find command
if [ -s maint/f77tof90 ] ; then
    if find maint -name 'f77tof90.in' -newer 'maint/f77tof90' >/dev/null 2>&1 ; then
        run_configure=yes
    fi
else
    run_configure=yes
fi
if [ "$run_configure" = "yes" ] ; then
    (cd maint && ./configure)
fi


# Create and/or update the f90 tests
if [ -x maint/f77tof90 -a $do_f77tof90 = "yes" ] ; then
    echo "Create or update the Fortran 90 tests derived from the Fortran 77 tests"
    for dir in f77/* ; do
        if [ ! -d $dir ] ; then continue ; fi
	leafDir=`basename $dir`
        if [ ! -d f90/$leafDir ] ; then
	    mkdir f90/$leafDir
        fi
        maint/f77tof90 $dir f90/$leafDir Makefile.sm Makefile.ap
    done
fi

#
# Create the Makefile.in files
# Make sure that these files exist so that the gcc dependency creation
# can work
rm_prepost=no
if [ ! -s src/include/mpidpre.h ] ; then
    rm_prepost=yes
fi

# Create the configure files and run autoheader
# Eventually, make this a test for find available.  Perhaps
# find . -name configure.ac > /dev/null 2>&1
# The problem is that even though cygwin has find, the DOS find
# is identified first.  We probably need a test for this case
fixBackWhackCtrlMBug=no
if [ $do_build_configure = yes ] ; then
    # If we don't delete the autom4te.cache files, bugs in 
    # autoconf may fail to correctly update configure
    # Gah. Some xargs don't accept -r, other break if there is no
    # input.  To avoid that, we do this in two steps: first the
    # find, then the rm (if there are any files)
    $autoreconf -vif $acIncDirFlag confdb
    if [ ! -x configure ] ; then 
        # Check for a few allowed exceptions
        echo "Could not build configure from configure.ac in $dir"
	echo "Aborting updatefiles!"
	exit 1
    fi
fi

#
# The following must be the last statements executed
if [ -n "$error_summary" ] ; then
    echo " "
    echo "Problems encountered while running updatefiles."
    echo "These may cause problems when configuring or building MPICH."
    echo "$error_summary"
fi
case $finalStatus in 
   0) # all is well
	;;
   1) # warnings
 	;;
   2) # reserved for future use
	;;
   3) # fatal, cannot continue
	echo "updatefiles step failed!  Review the output and fix the problem"
        echo "before continuing"
        exit 1
	;;	
   *) # unknown
	echo "Internal error in updatefiles; finalStatus=$finalStatus"
	exit 1
	;;
esac

